package java0418;

import java.util.*;

class A implements Comparable<A> {
    public int a;
    public double b;

    public A(int a, double b) {
        this.a = a;
        this.b = b;
    }

    @Override
    public int hashCode() {
        Integer aInt = new Integer(a);
        Double bDo = new Double(b);
        return aInt.hashCode() + bDo.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        A a1 = (A)obj;
        if (a == a1.a && b == a1.b) {
            return true;
        }
        return false;
    }

    @Override
    public int compareTo(A o) {
        if (a == o.a) {
            if (b == o.b) {
                return 0;
            }else if (b > o.b) {
                return 1;
            }else {
                return -1;
            }
        }else {
            return a - o.a;
        }
    }

    @Override
    public String toString() {
        return "" + a + " " + b;
    }
}

public class Test {

    public static void main(String[] args) {
        Set<A> set1 = new HashSet<>();
        Set<A> set2 = new TreeSet<>();
        A a1 = new A(1, 2.1);
        A a2 = new A(1, 2.1);
        set1.add(a1);
        set1.add(a2);
        set2.add(a1);
        set2.add(a2);
        System.out.println("a1 的哈希码为" + a1.hashCode());
        System.out.println("a2 的哈希码为" + a2.hashCode());
        System.out.println("a1 和 a2 是否相等" + a1.equals(a1));
        System.out.println("a1 和 a2 的大小关系为" + a1.compareTo(a2));
        System.out.println("HashSet 的大小为" + set1.size());
        System.out.println("TreeSet 的大小为" + set2.size());
        A a3 = new A(10, 2.9);
        A a4 = new A(7, 3.1);
        List<A> list = new ArrayList<>();
        list.add(a1);
        list.add(a2);
        list.add(a3);
        list.add(a4);
        Collections.sort(list);
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }
}
